<template>
  <div class="login">
    <el-dialog
      title="登录"
      :visible.sync="dialogFormVisible"
      width="20%"
      center
      @close="closeDia"
    >
      <el-form
        :model="registerForm"
        :rules="sterFormRules"
        ref="registerFormRef"
        status-icon
      >
        <!-- 账号 -->
        <el-form-item prop="userName">
          <el-input
            v-model="registerForm.userName"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="registerForm.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-view"
          ></el-input>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item>
          <el-button type="primary" @click="addLogin" style="width: 100%">登录</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import axios from "../api/loginOnRegApi";
export default {
  name: "LoginName",
  data() {
    return {
      dialogFormVisible: false,
      registerForm: {
        password: "",
        userName: "",
      },
      // 验证规则
      sterFormRules: {
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 18,
            message: "字母开头,长度5-16之间,允许字母数字下划线",
            trigger: "blur",
          },
        ],
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 5,
            max: 16,
            message: "字母开头,长度5-16之间,允许字母数字下划线",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 对话框开关
    showDialog() {
      this.dialogFormVisible = true;
    },
    // 关闭对话框回调  
    closeDia() {
      this.$refs.registerFormRef.resetFields();
    },
    // 登录
    addLogin() {
      this.$refs.registerFormRef.validate(async (valid) => {
        if (!valid) return;
        let { data: res } = await axios.login(this.registerForm);
        console.log(res);
        if (res.code == "001") {
          console.log(res);
          this.$store.commit("addUser", res.user);
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
          });
        } else {
          this.$notify.error({
            title: "失败",
            message: res.msg,
          });
        }
      });
      this.dialogFormVisible = false;
    },
  },
};
</script>
<style lang="scss" scoped></style>
